//
//  HELStatisticVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/7/13.
//  Copyright © 2016年 陈武琦. All rights reserved.
//
#import "HELMeetingInfo.h"
#import "HELMeetingInfoCell.h"
#import "HELWebViewController.h"
#import "HELSuperTableView.h"
#import "Masonry.h"

static NSString *cellID = @"HELMeetingInfoCellID";
@interface HELMeetingInfo ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;
@property(nonatomic, strong) HELMeetingInfoCell *cell;
//上拉加载的页面
@property (nonatomic, assign) NSInteger page;

@end

@implementation HELMeetingInfo

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"会议信息"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELMeetingInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
    weakSelf_define;
    // 添加下拉刷新头部控件
    [self.tableView wsj_HeaderRefreshBlock:^{
        //请求加载数据
        weakSelf.page = 1;
        [weakSelf requestData];
    }];

    [self.tableView addLegendFooterWithRefreshingBlock:^{
        weakSelf.page++;
        //请求加载数据
        [weakSelf requestData];
    }];
    self.page = 1;
    [self requestData];
}

- (void)requestData {
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    self.tableView.loading = YES;
    NSDictionary *param = @{@"pageSize":@20, @"currentPage":@(self.page), @"userId":userInfo.userId};
       defineWeakSelf;
    [[HELNetworkManager sharedMg] POST:helMeetingUrl parameters:param success:^(id responseObject) {
        if (responseObject[@"exception"]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *dict in responseObject[@"rows"]) {
                HELMeetingModel *model = [HELMeetingModel mj_objectWithKeyValues:dict];
                [arr addObject:model];
            }
            //判断是上拉刷新？  还是下拉加载？
            if (weakSelf.page > 1) {
                NSMutableArray *muArr = [weakSelf.dataArray mutableCopy];
                
                [muArr addObjectsFromArray:arr];
                weakSelf.dataArray = [muArr copy];
                if (weakSelf.page == [responseObject[@"pages"] integerValue]) {
                    weakSelf.tableView.footer.hidden = YES;
                }
                [weakSelf.tableView.footer endRefreshing];
            }else {
                
                if (weakSelf.page == [responseObject[@"pages"] integerValue]) {
                    weakSelf.tableView.footer.hidden = YES;
                }else {
                    weakSelf.tableView.footer.hidden = NO;
                }
                weakSelf.dataArray = arr;
                [weakSelf.tableView.header endRefreshing];
            }
            if (weakSelf.dataArray.count<=0) {
                weakSelf.tableView.loading = NO;
            }

            [weakSelf.tableView reloadData];
            
        }
    } failure:^(NSString *err) {
        if (weakSelf.page > 1) {
            //上拉
            [weakSelf.tableView.footer endRefreshing];
            weakSelf.page -= 1;
        }else {
            //下拉
            [weakSelf.tableView.header endRefreshing];
            if (weakSelf.dataArray.count<=0) {
                self.tableView.footer.hidden = YES;
            }else {
                
                self.tableView.footer.hidden = NO;
            }
        }
        cwqHudRequestError(err);
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }

    }];
    
    
}



- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kAppBg;
        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}
#pragma mark - <UITableViewData and UITableDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HELMeetingModel *model = self.dataArray[indexPath.row];
//    CGFloat titleLabelH =[model.title boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
//    
//    CGFloat height = titleLabelH + 30+1;
//    height = height>64 ? height:64;
    
    
    self.cell = (HELMeetingInfoCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    
    
    HELMeetingInfoCell *cell = self.cell;
    cell.model = model;
    
    //这句代码必须要有，也就是说必须要设定contentView的宽度约束。
    //设置以后，contentView里面的内容才知道什么时候该换行了
    CGFloat contentViewWidth = CGRectGetWidth(self.tableView.frame);
    [cell.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(contentViewWidth));
    }];
    
    //重新加载约束
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    
    //自动算高度，+1的原因是因为contentView的高度要比cell的高度小1
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
    
    return height;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELMeetingInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    HELMeetingModel *model = self.dataArray[indexPath.row];
    [cell setModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    return cell;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }

    HELMeetingModel *model = self.dataArray[indexPath.row];
    HELWebViewController *webVC = [[HELWebViewController alloc] init];
    NSString *url = [[[HELNetworkManager sharedMg] getBaseUrl] stringByAppendingString:helMeetingDeatailUrl];
    url = [url stringByAppendingFormat:@"?noticeId=%@&userId=%@",model.id,userInfo.userId];
    webVC.url = [NSURL URLWithString:url];
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
